package rjgc.demo.Utils;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import rjgc.demo.entity.CommodityEntity;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

public class UtilsparsePuPu {

    public static  CommodityEntity parsePuPu(String url) throws IOException, ParseException {
        LinkedTreeMap data ;
        String name;
        String spec;
        Double price;
        Double marketprice;
        String content;

        Connection.Response res = Jsoup.connect(url)
                .header("Accept", "*/*")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                .header("Content-Type", "application/json;charset=UTF-8")
                .timeout(10000).ignoreContentType(true).execute();
        //创建gson对象
        Gson gson = new Gson();
        //对获取到的数据进行转换
        HashMap hashMap = gson.fromJson(res.body(), HashMap.class);
        //获取data里面的值
        data = (LinkedTreeMap) hashMap.get("data");

        /* 获取对应数据 */
        name= (String) data.get("name");
        spec=(String) data.get("spec");
        price=(Double) data.get("price")/100;
        marketprice=(Double) data.get("market_price")/100;
        content=(String) data.get("share_content");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime =LocalDateTime.now();
        String formattedDateTime = dateTime.format(formatter);

        CommodityEntity commodity=new CommodityEntity(name,spec,price,marketprice,content,
                formattedDateTime);
        return commodity;

    }
//    public void timing() throws IOException, InterruptedException {
//        //每3秒抓取一次商品信息
//        while (true){
//            parsePuPu();
//            System.out.println("当前时间为"+ LocalDateTime.now()+" ,"+name+":价格为"+price);
//            Thread.sleep(3000);
//        }
//    }
//
//    public void show() throws InterruptedException, IOException {
//        parsePuPu();
//        //打印
//        System.out.println("-----------------------商品："+name+"-----------------------");
//        System.out.println("规格："+spec);
//        System.out.println("价格："+price);
//        System.out.println("原价/折扣价："+market_price+"/"+price);
//        System.out.println("详细信息："+content);
//        System.out.println("--------------------"+name+"的价格波动--------------------");
//        timing();
//    }



}
